<?php

namespace App\Models\video;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

// 数据缓存并存储数据库
class DataCacheModel extends Model
{
    protected $table = "up_cache"; //表名
    const CREATED_AT = null;
    const UPDATED_AT = null;
    static $userWatchMark = "userWatchMark";
    static $userDynamicViewMark = "userDynamicViewMark";

    //保存
    public static function saveCache($data){
        if(!is_array($data) || empty($data["key"]) || empty($data["value"]))
            return false;
        $value = $data["value"];
        $data["value"] = serialize($data["value"]);
        if(!empty($data["time"])){
            $option = $data["time"]/60;
            $data["time"] = time() + $data["time"];
        }else{
            $option = 100000;
        }
        if(!isset($data["mall_id"])) {
            $data["mall_id"] = request()->mallData["id"];
        }
        if(Cache::has("{$data["mall_id"]}_isExist_c_{$data["key"]}") || self::query()->where(["key" => $data["key"], "mall_id" => $data["mall_id"]])->count()){ //更新
            if(self::query()->where(["key" => $data["key"], "mall_id" => $data["mall_id"]])->update($data)) {
                Cache::put("{$data["mall_id"]}_".$data["key"], $value, $option);
                Cache::put("{$data["mall_id"]}_"."isExist_c_{$data["key"]}", true, $option);
                return true;
            }
        }else{
            if(self::query()->insert($data)){
                Cache::put("{$data["mall_id"]}_".$data["key"], $value, $option);
                Cache::put("{$data["mall_id"]}_"."isExist_c_{$data["key"]}", true, $option);
                return true;
            }
        }
        return false;
    }

    //获取某字段的值
    public static function getValue($key, $del = true){
        $mall_id = request()->mallData["id"];
        if(!$data = Cache::get("{$mall_id}_".$key)){
            $item = self::query()->where(["key" => $key, "mall_id" => $mall_id])->select(["time", "key", "value"])->first();
            if(!empty($item)){
                if(!empty($item["time"]) && $item["time"] < time()){ // 过期了
                    if($del) {
                        self::del($key);
                    }
                }else {
                    $data = unserialize($item["value"]);
                    Cache::put("{$mall_id}_".$key, $data, !empty($item["time"]) ? ($item["time"] - time())/60 : null);
                }
            }
        }
        return $data;
    }

    //根据删除数据
    public static function del($key){
        if (!$key) {
            return false;
        }
        $mall_id = request()->mallData["id"];
        Cache::forget("{$mall_id}_".$key);
        Cache::forget("{$mall_id}_"."isExist_c_{$key}");

        $result = self::query()->where("key", $key)->where("mall_id", $mall_id)->delete();

        //删除过期的数据
        $key = self::query()->where("time", "<", time())->where("mall_id", $mall_id)->select(["key"])->get();
        if(!empty($key)){
            foreach ($key as $id){
                Cache::forget("{$mall_id}_".$id["key"]);
                Cache::forget("{$mall_id}_"."isExist_c_{$id["key"]}");
            }
            $result = self::query()->where("time", "<", time())->where("mall_id", $mall_id)->select(["key"])->delete();
        }
        return $result;
    }

    //根据删除数据
    public static function delByVague($key){
        if (!$key) {
            return false;
        }
        $mall_id = request()->mallData["id"];
        $data = self::where("key", "like", "{$key}%")->where("mall_id", $mall_id)->select("key")->get();
        $result = self::where("key", "like", "{$key}%")->where("mall_id", $mall_id)->delete();
        if($result){
            foreach ($data as $item){
                Cache::forget("{$mall_id}_".$item["key"]);
                Cache::forget("{$mall_id}_"."isExist_c_{$item["key"]}");
            }
        }
        return $result;
    }
}